package org.durcframework.autocode.generator;

import org.durcframework.autocode.util.FieldUtil;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * SQL上下文,这里可以取到表,字段信息<br>
 * 最终会把SQL上下文信息放到velocity中
 */
public class SQLContext {
    private TableDefinition tableDefinition; // 表结构定义
    private String packageName; // 包名
    private String filterStr = "";// 需要过滤的字符串名
    private String packageSpace;// 特定注解使用的项目作用域：一般为包名的最后一个字符串

    public SQLContext(TableDefinition tableDefinition) {
        this.tableDefinition = tableDefinition;
        // 默认为全字母小写的类名
        this.packageName = getJavaBeanName().toLowerCase();
    }

    public SQLContext() {
        // TODO Auto-generated constructor stub
    }

    /**
     * 返回Java类名
     *
     * @return
     */
    public String getJavaBeanName() {
        String tableName = getJavaBeanNameLF();

        return FieldUtil.upperFirstLetter(tableName);
    }

    /**
     * 针对余老板mysql 表名业务分类，单个字母_开头情况过滤
     *
     * @param filterStr
     * @return
     */
    public String replaceFilterStr(String filterStr) {
        Pattern p = Pattern.compile("^[A-Za-z]_");
        Matcher m = p.matcher(filterStr);

        return m.replaceFirst("");
    }

    /**
     * 针对余老板mysql 表名业务分类，单个字母_开头情况过滤
     *
     * @param filterStr
     * @return
     */
    public String replaceFilterDDcStr(String filterStr) {
        String str[] = filterStr.split("\\_");
        String tableName = "";
        for (int i = 1; i < str.length; i++) {
            if (i == 1) {
                tableName += str[i];
            } else {
                tableName += "_" + str[i];
            }
        }

        return tableName;
    }

    public String replaceFilterSqlserver(String filterStr) {
        String str[] = filterStr.split("\\.");

        return str[str.length - 1];
    }

    /**
     * 返回Java类名且首字母小写
     *
     * @return
     */
    public String getJavaBeanNameLF() {
        String tableName = tableDefinition.getTableName();
        try {
            tableName = replaceFilterDDcStr(tableName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tableName = FieldUtil.underlineFilter(tableName);
        tableName = FieldUtil.dotFilter(tableName);
        return FieldUtil.lowerFirstLetter(tableName);
    }

    public String getPkName() {
        if (this.tableDefinition.getPkColumn() != null) {
            return this.tableDefinition.getPkColumn().getColumnName();
        }
        return "";
    }

    public String getJavaPkName() {
        if (this.tableDefinition.getPkColumn() != null) {
            return this.tableDefinition.getPkColumn().getJavaFieldName();
        }
        return "";
    }

    public String getJavaPkType() {
        if (this.tableDefinition.getPkColumn() != null) {
            return this.tableDefinition.getPkColumn().getJavaType();
        }
        return "";
    }

    public String getMybatisPkType() {
        if (this.tableDefinition.getPkColumn() != null) {
            return this.tableDefinition.getPkColumn().getMybatisJdbcType();
        }
        return "";
    }

    public TableDefinition getTableDefinition() {
        return tableDefinition;
    }

    public void setTableDefinition(TableDefinition tableDefinition) {
        this.tableDefinition = tableDefinition;
    }

    public List<ColumnDefinition> getColumnDefinitionList() {
        return tableDefinition.getColumnDefinitions();
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        if (packageName.indexOf(".") > 0) {
            String packageSpace = packageName.substring(packageName.lastIndexOf(".") + 1);
            setPackageSpace(packageSpace);
        }
        this.packageName = packageName;
    }

    public String getFilterStr() {
        return filterStr;
    }

    public void setFilterStr(String filterStr) {
        this.filterStr = filterStr;
    }

    public static void main(String[] args) {

        SQLContext[] tt = {new SQLContext(new TableDefinition("k_stock_in_item")),
                new SQLContext(new TableDefinition("dbo.am_kind")),
                new SQLContext(new TableDefinition("user.frontUser")),
                new SQLContext(new TableDefinition("user.back_user"))};

        for (SQLContext ctx : tt) {
            System.out.println(ctx.getJavaBeanName());

        }

        /*
         * String pname = "com.dian.yosemite.service.brp"; String packageSpace =
         * pname.substring(pname.lastIndexOf(".")+1); System.out.println(packageSpace);
         */
        /*
         * 输出: RTableB MyTable UserFrontUser UserBackUser
         */
    }

    public String getPackageSpace() {
        return packageSpace;
    }

    public void setPackageSpace(String packageSpace) {
        this.packageSpace = packageSpace;
    }

}
